<template>
<div>
    <!-- header区域 -->
    <header class="category-header">
        <i class="iconfont icon-left" @click="goback"></i>
        <div class="header-search">
            <i class="iconfont icon-search"></i>
            <span class="search-title">双11随心所欲购</span>
        </div>
        <i class="iconfont icon-More"></i>
    </header>

    <!-- 分类的内容区域 -->
    <section class="category-wrap">
        <!-- 左侧分类导航 -->
        <div class="nav-side-wrapper" ref="navWrapper">
            <ul class="nav-side">
                <li :class="{active:index == i}" v-for="(item,i) in categoryList" :key="i" @click="selectMenu(i)">{{item.name}}</li>
            </ul>
            <!-- 这里可以放一些其它的 DOM，但不会影响滚动 -->
        </div>
        <!-- 右侧分类内容 -->
        <div class="content-wrapper" ref="contentWrapper">
            <div class="content-slide" v-for="(category,i) in categories" :key="i">
                <img :src="category.mainImgUrl" class="category-main-img" v-if="category.mainImgUrl">
                <div class="category-list" v-for="(categoryItem,i) in category.list" :key="i">
                    <p class="catogory-title">{{categoryItem.title}}</p>
                    <div class="product-item" v-for="(product,i) in categoryItem.productList" :key="i" @click="goProductList">
                        <img :src="product.imgUrl" class="product-img">
                        <p class="product-title">{{product.title}}</p>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <!-- 底部标签栏tabbar -->
    <tabbar></tabbar>
</div>
</template>

<script>
// 1. 导入tabbar子组件
import tabbar from '../../components/tabbar.vue'
import BScroll from 'better-scroll'
import {
    getCategoryList
} from '@/api'
export default {
    // 2. 将tabbar组件注册成为home的子组件
    components: {
        tabbar
    },
    data() {
        return {
            categoryList: [],
            index: 0
        }
    },
    created() {
        this.fetchCategoryList()
    },
    computed: {
        categories: function () {
            // 过滤categoryList 只留下一个元素，留下索引和index相对的元素
            return this.categoryList.filter((v, i) => this.index === i)
        }
    },
    methods: {
        goProductList() {
            this.$router.push('/product-list?categoryId=100008')
        },
        goback() {
            this.$router.go(-1)
        },
        selectMenu(i) {
            console.log(i)
            this.index = i
        },
        fetchCategoryList() {
            getCategoryList().then(res => {
                this.categoryList = res

                this.$nextTick(() => {
                    this.setWrapperHeight()
                    let sideWrapper = document.querySelector('.nav-side-wrapper')
                    let contentWrapper = document.querySelector('.content-wrapper')
                    new BScroll(sideWrapper)
                    new BScroll(contentWrapper)
                })
            })
        },
        setWrapperHeight() {
            const height = window.screen.height - 100
            this.$refs.navWrapper.style.height = height + 'px'
            this.$refs.contentWrapper.style.height = height + 'px'
        }
    }
}
</script>

<style lang="scss" scoped>
@import '@/common/style/mixin.scss';

.category-header {
    position: fixed;
    left: 0;
    top: 0;
    @include fj;
    width: 100%;
    height: 100px;
    line-height: 100px;
    padding: 0 30px;
    @include boxSizing;
    font-size: 30px;
    color: #656771;
    z-index: 10000;

    .icon-left,
    .icon-More {
        font-size: 50px;
    }

    .header-search {
        display: flex;
        width: 74%;
        height: 40px;
        line-height: 40px;
        margin: 20px 0;
        padding: 10px 0;
        color: #232326;
        background: #f7f7f7;
        @include borderRadius(40px);

        .icon-search {
            padding: 0 20px;
            font-size: 34px;
        }

        .search-title {
            font-size: 24px;
            color: #666;
        }
    }

    .icon-iconyonghu {
        font-size: 44px;
    }
}

.category-wrap {
    @include fj;
    width: 100%;
    margin-top: 100px;
    background: #f8f8f8;
    border-top: 1px solid #dcdcdc;

    .nav-side-wrapper {
        width: 28%;
        height: 100%;
        overflow: hidden;

        .nav-side {
            width: 100%;
            @include boxSizing;
            background: #f8f8f8;

            li {
                width: 100%;
                height: 92px;
                text-align: center;
                line-height: 92px;
                font-size: 28px;

                &.active {
                    color: $red;
                    background: #fff;
                }
            }
        }
    }
}

.content-wrapper {
    width: 72%;
    height: 100%;
    overflow: hidden;

    .content-slide {
        width: 100%;
        padding: 0 20px;
        background: #fff;
        @include boxSizing;

        .category-main-img {
            width: 100%;
        }

        .category-list {
            display: flex;
            flex-wrap: wrap;
            flex-shrink: 0;
            width: 100%;

            .catogory-title {
                width: 100%;
                font-size: 34px;
                font-weight: 500;
                padding: 40px 0;
            }

            .product-item {
                width: 33.3333%;
                margin-bottom: 20px;
                text-align: center;
                font-size: 30px;

                img {
                    width: 100%;
                }
            }
        }
    }
}
</style>
